home *** CD-ROM | disk | FTP | other *** search
- #include "42.h"
- #include "42_plugin_manager.h"
- #include "42_plugin_life.h"
-
- #include "LifeModule.h"
- #include "G4Monitor.h"
-
- /* plugin definition */
-
- forty_two_plugin_api plugin_life =
- {
- life_init, // init
- life_kill, // kill
- life_idle, // idle1
- nil, // idle2
- life_click, // click
- life_ipc, // ipc
- nil, // update
- 1, // req_dialog_item
- (GWorldPtr) 0L // port
- };
-
- /* plugin functions */
-
- unsigned long *life_base;
- Handle ThePrivateData;
- unsigned long life_gen = 0;
- unsigned long life_generation = 0;
-
- int life_init(void)
- {
- int x,y;
- long rowbytes;
-
- G4SetMMCR0( kMMCR0_PMC1_processor_cycles, 0, 0);
-
- life_base = (unsigned long *) (**(plugin_life.port->portPixMap)).baseAddr;
- rowbytes = (**(plugin_life.port->portPixMap)).rowBytes & 0x3fff;
-
- ThePrivateData = NewHandle(0);
- PICreateLife(ThePrivateData, life_base, rowbytes, 0);
-
- for(x = 0; x < 128; x++)
- for(y = 0; y < 128; y++)
- PISetACell(ThePrivateData,x, y, 0);
-
- PIDrawBoard(ThePrivateData);
-
- return GetHandleSize(ThePrivateData);
- }
-
- extern void SetTextBox(short item, unsigned char *str);
-
- int life_idle(void)
- {
- long lg, ld;
- unsigned char lgs[32],lds[32];
- unsigned char gs[32];
-
- if(life_gen)
- {
- lg = life_generate();
- ld = life_draw();
- life_generation++;
- life_gen--;
-
- NumToString(lg,lgs);
- NumToString(ld,lds);
- NumToString(life_generation,gs);
-
- SetTextBox(cycles_generate,lgs);
- SetTextBox(cycles_draw,lds);
- SetTextBox(generations,gs);
- }
- }
-
- int life_generate(void)
- {
- signed long cycles, base, junk1;
-
- // Load timming routines into cache
- G4ClearPMC();
- G4GetPMC( &cycles, &junk1, &junk1, &junk1);
-
- G4ClearPMC();
- G4GetPMC( &base, &junk1, &junk1, &junk1);
-
- G4ClearPMC();
- G4GetPMC( &base, &junk1, &junk1, &junk1);
-
- // Measure the PIGeneeration routine
- G4ClearPMC();
-
- PINextGeneration(ThePrivateData);
-
- G4GetPMC( &cycles, &junk1, &junk1, &junk1);
- // subtract out the zero valuse
-
- cycles -= base;
-
- return (int)cycles;
- }
-
- int life_draw(void)
- {
- signed long cycles, base, junk1;
-
- // Load timming routines into cache
- G4ClearPMC();
- G4GetPMC( &cycles, &junk1, &junk1, &junk1);
-
- G4ClearPMC();
- G4GetPMC( &base, &junk1, &junk1, &junk1);
-
- G4ClearPMC();
- G4GetPMC( &base, &junk1, &junk1, &junk1);
-
- // Measure the PIGeneeration routine
- G4ClearPMC();
-
- PIDrawBoard(ThePrivateData);
-
- G4GetPMC( &cycles, &junk1, &junk1, &junk1);
- // subtract out the zero valuse
-
- cycles -= base;
-
- (*plugin_life.update)(&plugin_life);
-
- return (int)cycles;
- }
-
- int life_click(Point *localpt)
- {
- Boolean cell;
-
- cell = PIReadACell(ThePrivateData,localpt->h,localpt->v);
- PISetACell(ThePrivateData,localpt->h,localpt->v,!cell);
- PIDrawBoard(ThePrivateData);
- plugin_life.update(&plugin_life);
- }
-
- int life_kill(void)
- {
- PITrashLife(ThePrivateData);
- }
-
- int life_ipc(int sel, unsigned long msg)
- {
- switch(sel)
- {
- case LIFE_SETCOLOR:
- life_setcolor(msg);
- break;
- case LIFE_LOAD:
- life_load(msg);
- life_generation = 0;
- break;
- case LIFE_START:
- life_gen = 99999;
- break;
- case LIFE_STOP:
- life_gen = 0;
- break;
- case LIFE_STEP:
- life_gen = 1;
- break;
- }
- }
-
- void life_setcolor(int color)
- {
- PISetColor(ThePrivateData, color);
- PIDrawBoard(ThePrivateData);
- (*plugin_life.update)(&plugin_life);
- }
-
- void life_load(int sel)
- {
- OSErr err;
- long x,xx,y,yy;
- short refNum;
- FSSpec theSpec;
- long bytes;
-
-
- switch (sel)
- {
- case 1:
- err = FSMakeFSSpec(0,0,"\p42a.life",&theSpec);
- if (err != noErr) return;
- break;
- case 2:
- err = FSMakeFSSpec(0,0,"\p42b.life",&theSpec);
- if (err != noErr) return;
- break;
- case 3:
- err = FSMakeFSSpec(0,0,"\p42c.life",&theSpec);
- if (err != noErr) return;
- break;
- case 4:
- err = FSMakeFSSpec(0,0,"\p42d.life",&theSpec);
- if (err != noErr) return;
- break;
- case 5:
- for(x = 0; x < 128; x++)
- for(y = 0; y < 128; y++)
- PISetACell(ThePrivateData,x, y, Random() % 2);
- PIDrawBoard(ThePrivateData);
- (*plugin_life.update)(&plugin_life);
- return;
- break;
- default:
- return;
- }
-
- err = FSpOpenDF(&theSpec, fsRdPerm, &refNum);
- if (err != noErr) return;
-
- bytes = 4;
- FSRead(refNum, &bytes, &y);
- bytes = 4;
- FSRead(refNum, &bytes, &x);
-
- if (x != 128)
- return;
-
- if (y != 128)
- return;
-
- // set cells
- for (xx = 0; xx < x; xx++)
- {
- for (yy = 0; yy < y; yy++)
- {
- char state;
- bytes = 1;
- FSRead(refNum, &bytes, &state);
- PISetACell(ThePrivateData,xx, yy,state);
- }
- }
- FSClose(refNum);
- PIDrawBoard(ThePrivateData);
- (*plugin_life.update)(&plugin_life);
- }